package com.xueyiyue.xueyiyue.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xueyiyue.xueyiyue.model.entity.*;
import com.xueyiyue.xueyiyue.model.sys.ApiResult;
import com.xueyiyue.xueyiyue.model.sys.UserOpenInfo;
import com.xueyiyue.xueyiyue.service.UserService;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import com.xueyiyue.xueyiyue.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenlixian
 * @description TODO
 * @date 2020/12/18 13:03
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 获取openId
     * @param userOpenInfo
     * @return
     **/
    @RequestMapping(value = "/getOpenId", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getOpenId(@RequestBody UserOpenInfo userOpenInfo) {
        userOpenInfo.setApp_id("wxde91489a1119efad");
        userOpenInfo.setSecret("a57f1bfc8019f5c228c715e1d3b02c98");
        String host = "https://api.weixin.qq.com";
        String path = "/sns/jscode2session";
        String method = "POST";
        String grant_type = "authorization_code";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        //根据API的要求，定义相对应的Content-Type
        headers.put("content-type", "application/json");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("appid", userOpenInfo.getApp_id());
        bodys.put("secret", userOpenInfo.getSecret());
        bodys.put("js_code", userOpenInfo.getJs_code());
        bodys.put("grant_type", grant_type);

        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
//            System.out.println(response.toString());
            //获取response的body
            JSONObject jsonObject = new JSONObject(EntityUtils.toString(response.getEntity()));
            String openid = jsonObject.getString("openid");
            System.out.println(openid);
            return ApiResult.ok(openid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 修改/新增用户信息
     * @param
     * @return
     **/
    @RequestMapping(value = "/enterUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult enterUserInfo(@RequestBody Map<String,UserInfo>  map) {
        Integer count = userService.enterUserInfo(map.get("userInfo"));
        if (count == 1)
            return ApiResult.ok("修改成功！");
        else
            return ApiResult.fail("修改失败！");
    }
    /**
     * 获取用户信息
     * @param
     * @return
     **/
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult enterUserInfoDetail(@RequestBody UserInfo userInfo) {
        UserInfo user = userService.getUserInfo(userInfo.getOpenId());
        return ApiResult.ok(user);
    }
    /**
     * 修改/新增用户详情
     * @param
     * @return
     **/
    @RequestMapping(value = "/enterUserInfoDetail", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult enterUserInfoDetail(@RequestBody Map<String, UserInfoDetail>  map) {
        Integer count = userService.enterUserInfoDetail(map.get("userInfoDetail"));
        if (count == 1)
            return ApiResult.ok("修改成功！");
        else
            return ApiResult.fail("修改失败！");
    }
    /**
     * 获取字典
     * @param
     * @return
     **/
    @RequestMapping(value = "/getDicData", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getDicData(@RequestBody Dictionary dictionary) {
        List<Dictionary> list = userService.getDicData(dictionary);
        return ApiResult.ok(list);
    }
    /**
     * 获取用户详情
     * @param
     * @return
     **/
    @RequestMapping(value = "/getUserInfoDetail", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getUserInfoDetail(@RequestBody UserInfoDetail userInfoDetail) {
        UserInfoDetail userInfoDetail1 = userService.getUserInfoDetail(userInfoDetail);
        return ApiResult.ok(userInfoDetail1);
    }
    /**
     * 发布
     * @param
     * @return
     **/
    @RequestMapping(value = "/createNews", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult createNews(@RequestBody Map<String,NewsVo>  map) {
        Integer count = userService.createNews(map.get("news"));
        return ApiResult.ok(count);
    }
    /**
     * 发布兼职
     * @param
     * @return
     **/
    @RequestMapping(value = "/createJobs", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult createJobs(@RequestBody Map<String,JobsVo>  map) {
        Integer count = userService.createJobs(map.get("jobs"));
        return ApiResult.ok(count);
    }
    /**
     *获取找老师列表
     * @param
     * @return
     **/
    @RequestMapping(value = "/getNewsList", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getNewsList(@RequestBody Map<String,NewsListVo> map) {
        NewsListVo newsListVo = map.get("newsListVo");
        PageHelper.startPage(newsListVo.getPageNum(),newsListVo.getPageSize());
        List<NewsListVo> list = userService.getNewsList(newsListVo);
        PageInfo<NewsListVo> pageInfo = new PageInfo<NewsListVo>(list);
        return ApiResult.ok(pageInfo);
    }
    /**
     *获取兼职列表
     * @param
     * @return
     **/
    @RequestMapping(value = "/getJobsList", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getJobsList(@RequestBody Map<String,JobsListVo> map) {
        JobsListVo jobsListVo = map.get("jobsListVo");
        PageHelper.startPage(jobsListVo.getPageNum(),jobsListVo.getPageSize());
        List<JobsListVo> list = userService.getJobsList(jobsListVo);
        PageInfo<JobsListVo> pageInfo = new PageInfo<JobsListVo>(list);
        return ApiResult.ok(pageInfo);
    }
    /**
     *获取兼职列表
     * @param
     * @return
     **/
    @RequestMapping(value = "/deleteNews", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteNews(@RequestBody NewsVo newsVo) {
        Integer result = userService.deleteNews(newsVo);
        return ApiResult.ok(result);
    }
    /**
     *获取兼职列表
     * @param
     * @return
     **/
    @RequestMapping(value = "/deleteJobs", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteJobs(@RequestBody JobsVo jobsVo) {
        Integer result = userService.deleteJobs(jobsVo);
        return ApiResult.ok(result);
    }
    /**
     *获取兼职列表
     * @param
     * @return
     **/
    @RequestMapping(value = "/favorite", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult favorite(@RequestBody Favorite favorite) {
        Integer result = userService.favorite(favorite);
        return ApiResult.ok(result);
    }
    /**
     *获取收藏列表
     * @param
     * @return
     **/
    @RequestMapping(value = "/getFavoriteList", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getFavoriteList(@RequestBody Map<String,Favorite> map) {
        Favorite favorite = map.get("favorite");
        PageHelper.startPage(favorite.getPageNum(),favorite.getPageSize());
        if(favorite.getType()==2){
            List<JobsListVo> list = userService.getFavoriteJobsList(favorite);
            PageInfo<JobsListVo> pageInfo = new PageInfo<JobsListVo>(list);
            return ApiResult.ok(pageInfo);
        }
        else
        {
            List<NewsListVo> newsListVos = userService.getFavoriteNewsList(favorite);
            PageInfo<NewsListVo> pageInfo = new PageInfo<NewsListVo>(newsListVos);
            return ApiResult.ok(pageInfo);
        }


    }
}
